home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 143 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  109 lines

  1. Path: news.cis.nctu.edu.tw!usenet
  2. From: terryt@mcs.com (Terry Trippany)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: MSVC++ 4.0 __declspec( dllexport ) and templates
  5. Date: 2 Jan 1996 19:51:25 GMT
  6. Organization: STR/Baxter Labs
  7. Message-ID: <4cc2bt$4dd@news.cis.nctu.edu.tw>
  8. References: <00001a81+00008478@msn.com>
  9. NNTP-Posting-Host: @159.198.73.111
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In article <00001a81+00008478@msn.com>, sboag@msn.com says...
  15. >
  16. >I am having trouble exporting template classes, because either 
  17. MSVC++ 
  18. >4.0 is broke, or I'm broke in the head and don't understand 
  19. >something.  See the indented comment in the code below.  I've 
  20. tried 
  21. >lots and lots of things, but nothing seems to work.  Can anyone 
  22. lend 
  23. >a hand?
  24. >
  25. >#define DllExport       __declspec( dllexport )
  26. >
  27. >template<class T>
  28. >        // If I do this, I get a zillion errors:
  29. >        // class DllExport templateClass 
  30. >// if I do this, it compiles, but I can not use the 
  31. >// DllExport class in the dll client.
  32. >class templateClass 
  33. >{
  34. >public:
  35. >        templateClass(T val) : x(val) { }
  36. >        T x;
  37. >};
  38. >
  39. >class DllExport apiClass 
  40. >{
  41. >public:
  42. >        apiClass(int val) : a(val) { }
  43. >        templateClass<int> a;           
  44. >};
  45.  
  46. Hello,
  47.  
  48.   I have had experiences in this area and unfortunately have no 
  49. good news. The bottom line is that you can't export templatized 
  50. classes for use in a client with 4.0.  What you have to do is 
  51. put a wrapper around the template class and export that class.  
  52. I do suggest taking out the template in line constructor and 
  53. just adding the implementation to the bottom of the header file.
  54.   
  55. For Example:
  56.  
  57. // File : tempclass.hpp
  58.  
  59. template <class T>
  60. class templateClass
  61. {
  62.   public:
  63.     templateClass(T val);
  64.   private:
  65.     // provide a copy constructor
  66.     // you must have this to initialize x!
  67.     templateClass(const templateClass& anotherClass);  
  68.     T x;
  69. };
  70.  
  71. template < class T >
  72. templateClass<T>::templateClass(T val) : x(val)
  73. {
  74.  
  75. }
  76.  
  77. template < class T >
  78. templateClass<T>::templateClass(const templateClass& 
  79. anotherClass)
  80. {
  81.   x = anotherClass.x;  // assuming class T has a good copy 
  82. constructor
  83. }
  84.  
  85.  
  86. // File apiclass.hpp
  87.  
  88. // you can export and use the following class!
  89.  
  90. class DllExport apiClass 
  91. {
  92.  
  93. public:
  94.     apiClass(int val) : a(val) {};   
  95.  
  96.         // add manipulators for private type a here
  97. private:
  98.     templateClass<int> a;
  99. };
  100.  
  101.  
  102. There are a lot of issues here but this works.  Good luck.  
  103.  
  104. Terry Trippany
  105. terryt@mcs.com
  106. Strategic Technology Resources
  107. Chicago, Il 
  108.  
  109.